package com.neuron.platform;

import com.neuron.platform.tools.Influxdb4RTTools;
import io.modelcontextprotocol.client.McpClient;
import io.modelcontextprotocol.client.transport.ServerParameters;
import io.modelcontextprotocol.client.transport.StdioClientTransport;
import io.modelcontextprotocol.spec.McpSchema;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
public class MainClientStdio {
    public static final String url = "https://influxdb2.test.neuroncloud.ai";

    public static final String token = "Ye5x_TNwXavafa2mHjpUVXS697MUVKtyIbIvIcBTVGVzjYgAdG0O556j4HMqFtu-hLk6jwu_Izvsj_sEYLIO7g==";

    public static final String org = "neuron";

    public static final String bucket = "application_realtime_db";

    @Test
    public void test1() {
        System.out.println("Hello World!"+ Instant.now());
        Map<String, String> env = new HashMap();
        env.put("INFLUXDB_TOKEN", token);
        env.put("INFLUXDB_URL", url);
        env.put("INFLUXDB_ORG", org);

        var stdioParams = ServerParameters.builder("java")
                .args("-jar", "target/spring-mcp-server-0.0.1-SNAPSHOT.jar")
                .env(env)
                .build();
        stdioParams.getArgs().forEach(System.out::println);
        stdioParams.getEnv().forEach((key, value) -> System.out.printf("%s=%s%n", key, value));

        var transport = new StdioClientTransport(stdioParams);
        var client = McpClient.sync(transport).build();

        client.initialize();

        var toolsList = client.listTools();
        System.out.printf("toolsList = %s%n", toolsList);


        McpSchema.CallToolResult data = client.callTool(new McpSchema.CallToolRequest("query_equip_data",
                Map.of("tenantCode", "3000004",
                        "equipmentName","24e124136c450828",
                        "functionType", "temperature",
                        "startTime", "1m",
                        "endTime", "")));
        System.out.printf("equip data =>\n %s%n", data);

        client.closeGracefully();
    }

    @Test
    public void test2() {
        String url = "https://influxdb2.test.neuroncloud.ai";
        String token = "Ye5x_TNwXavafa2mHjpUVXS697MUVKtyIbIvIcBTVGVzjYgAdG0O556j4HMqFtu-hLk6jwu_Izvsj_sEYLIO7g==";
        String org = "neuron";
        Influxdb4RTTools client = new Influxdb4RTTools(url, token, org);
        System.out.println(client.getEquipRealtimeData("3000004", "24e124136c450828", null, "15m", null));
    }
}
